<template>
  <div class="department-container">
    <div class="app-container">
      <template>
        <div class="department-container">
          <div class="app-container">
            <el-card>
              <!-- 用一个行列布局 -->
              <el-row type="flex" justify="space-between" align="middle" style="height: 40px">
                <el-col :span="20">
                  <svg-icon icon-class="home" /><span>江苏传智播客教育科技股份有限公司</span>
                </el-col>
                <el-col :span="4">
                  <el-row type="flex" justify="end">
                    <!-- 两个内容 -->
                    <el-col>负责人</el-col>
                    <!-- 下拉菜单 element -->
                    <el-col>
                      <el-dropdown>
                        <span>
                          操作<i class="el-icon-arrow-down" />
                        </span>
                        <el-dropdown-menu slot="dropdown">
                          <el-dropdown-item>添加子部门</el-dropdown-item>
                        </el-dropdown-menu>
                      </el-dropdown>
                    </el-col>
                  </el-row>
                </el-col>
              </el-row>
              <el-tree
                :data="list"
                :default-expand-all="true"
              >
                <!-- 作用域插槽 data拿到的是每一个子节点的对象 -->
                <!--
      #default === slot-scope
      什么时候用到作用域插槽？父组件中如果想使用子组件中的数据进行自定义内容的渲染 (类似于table 单元格数据渲染)
    -->
                <template #default="{ data }">
                  <el-row
                    type="flex"
                    justify="space-between"
                    align="middle"
                    style="height: 40px; width: 100%;"
                  >
                    <el-col :span="20">
                      <span>{{ data.name }}</span>
                    </el-col>
                    <el-col :span="4">
                      <el-row type="flex" justify="end">
                        <!-- 两个内容 -->
                        <el-col>{{ data.manager }}</el-col>
                        <el-col>
                          <!-- 下拉菜单 element -->
                          <el-dropdown>
                            <span> 操作<i class="el-icon-arrow-down" /> </span>
                            <!-- 下拉菜单 -->
                            <el-dropdown-menu slot="dropdown">
                              <el-dropdown-item @click.native="hAdd(data.id)">添加子部门</el-dropdown-item>
                              <el-dropdown-item @click.native="hEdit(data.id)">编辑部门</el-dropdown-item>
                              <el-dropdown-item @click.native="doDel(data.id)">删除部门</el-dropdown-item>
                            </el-dropdown-menu>
                          </el-dropdown>
                        </el-col>
                      </el-row>
                    </el-col>
                  </el-row>
                </template>
              </el-tree>
            </el-card>
            <el-dialog
              title="提示"
              :visible.sync="showDialog"
              width="50%"
              @closed="resetForm"
            >
              <Dialog
                :id="curId"
                ref="dptForm"
                :is-edit="isEdit"
                :origin-list="originList"
                @success="success"
                @close="close"
              />
              <!-- <span slot="footer" class="dialog-footer">
                <el-button @click="showDialog = false">取 消</el-button>
                <el-button type="primary" @click="showDialog = false">确 定</el-button>
              </span> -->
            </el-dialog>

          </div>
        </div>
      </template>
    </div>
  </div>
</template>
<script>
import Dialog from './dialog.vue'
import { delDepartments, getDepartmentsList } from '@/api/departments'
import { tranListToTreeData } from '@/utils'
export default {
  components: {
    Dialog
  },
  data() {
    return {
      list: [], // tree中的数据
      showDialog: false,
      curId: '',
      isEdit: false,
      originList: []
    }
  },
  created() {
    this.loadingDepartments()
  },
  methods: {
    // 重置
    resetForm() {
      this.$refs.dptForm.reset()
    },
    // 提交后的收尾函数
    success() {
      this.showDialog = false
      this.loadingDepartments()
    },
    close() {
      this.showDialog = false
    },
    // 删除
    doDel(id) {
      this.$confirm('永久删除该部门, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        const res = await delDepartments(id)
        this.$message.success(res.message)
        this.loadingDepartments()
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    hEdit(id) {
      this.showDialog = true
      this.curId = id
      this.isEdit = true
      // 编辑数据回显
      this.$nextTick(() => {
        this.$refs.dptForm.getDepartmentInform()
      })
    },
    hAdd(id) {
      this.showDialog = true
      this.curId = id
      this.isEdit = false
    },
    async  loadingDepartments() {
      const res = await getDepartmentsList()
      // console.log(res.data.depts)
      // 处理数据传递给子组件用于数据校验
      this.originList = res.data.depts.map(item => {
        const { id, pid, name, code } = item
        return { id, pid, name, code }
      })
      this.list = tranListToTreeData(res.data.depts)
    // console.log(this.list)
    }
  }
}
</script>
